package l1j.server.server.model.skill;

import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.model.L1Character;

public class L1SkillTimerThreadImpl extends Thread implements L1SkillTimer {

	private final L1Character _cha;

	private final int _timeMillis;

	private final int _skillId;

	private int _remainingTime;

	public L1SkillTimerThreadImpl(final L1Character cha, final int skillId,
			final int timeMillis) {
		this._cha = cha;
		this._skillId = skillId;
		this._timeMillis = timeMillis;
	}

	@Override
	public void begin() {
		GeneralThreadPool.getInstance().execute(this);
	}

	@Override
	public void end() {
		super.interrupt();
		L1SkillStop.stopSkill(this._cha, this._skillId);
	}

	@Override
	public int getRemainingTime() {
		return this._remainingTime;
	}

	@Override
	public void kill() {
		if (Thread.currentThread().getId() == super.getId()) {
			return; // 呼び出し元スレッドが自分であれば止めない
		}
	}

	@Override
	public void run() {
		for (int timeCount = this._timeMillis / 1000; timeCount > 0; timeCount--) {
			try {
				Thread.sleep(1000);
				this._remainingTime = timeCount;
			} catch (final InterruptedException e) {
				return;
			}
		}
		this._cha.removeSkillEffect(this._skillId);
	}
}
